#include "SpriteTest.h"

SpriteTest::SpriteTest(HINSTANCE hInstance, std::string winCaption)
: D3DApp(hInstance, winCaption, D3DDEVTYPE_HAL, D3DCREATE_HARDWARE_VERTEXPROCESSING)
{
	Armadillo* a = new Armadillo();

	OnResetDevice();
}

SpriteTest::~SpriteTest()
{
}

void SpriteTest::OnLostDevice()
{
	D3DApp::OnLostDevice();
}

void SpriteTest::OnResetDevice()
{
	D3DApp::OnResetDevice();

	D3DXMATRIX view;
	D3DXMATRIX proj;
	D3DXVECTOR3 pos(0.0f, 0.0f, -1000.0f);
	D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
	D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);

	D3DXMatrixLookAtLH(&view, &pos, &target, &up);

	RECT r;
	GetClientRect(mhMainWindow, &r);

	//D3DXMatrixPerspectiveFovLH(&proj,
	//	-D3DX_PI*0.25f,
	//	(float)mD3Dpp.BackBufferWidth / (float)mD3Dpp.BackBufferHeight,
	//	1.0f, 5000.0f);

	D3DXMatrixOrthoLH(&proj,
		(float)mD3Dpp.BackBufferWidth,
		(float)mD3Dpp.BackBufferHeight, 1.0f, 5000.0f);

	HR(gD3DDevice->SetRenderState(D3DRS_LIGHTING, false));
	HR(gD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE));

	// pour le Alpha
	HR(gD3DDevice->SetRenderState(D3DRS_ALPHAREF, 10));
	HR(gD3DDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATER));

	// alpha blend
	HR(gD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE));
	HR(gD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1));
	HR(gD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA));
	HR(gD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA));

	HR(gD3DDevice->SetTransform(D3DTS_VIEW, &view));
	HR(gD3DDevice->SetTransform(D3DTS_PROJECTION, &proj));
}

void SpriteTest::Update(double dt)
{
	D3DApp::Update(dt);

	//std::cout << dt << std::endl;
	//
	//if (gDInput->keyPressed(DIK_SPACE))
	//{
	//	std::cout << "Yeah space Pressed!" << std::endl;
	//}
	//
	//if (gDInput->keyReleased(DIK_SPACE))
	//{
	//	std::cout << "Yeah space Release!" << std::endl;
	//}
}

void SpriteTest::Draw(ID3DXSprite& sprite, D3DXMATRIX& view, D3DXMATRIX& proj)
{
	HR(gD3DDevice->BeginScene());

	D3DApp::Draw(sprite, mView, mProj);

	HR(gD3DDevice->EndScene());
	HR(gD3DDevice->Present(0, 0, 0, 0));
}

